package com.xnyzc.lhy.mis.service.impl.cms.login;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xnyzc.lhy.common.component.config.GlobalConfig;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.ListToTreeUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.cms.OaSysMenu;
import com.xnyzc.lhy.mis.entity.param.cms.LoginInfo;
import com.xnyzc.lhy.mis.entity.system.OaSysUser;
import com.xnyzc.lhy.mis.mapper.cms.OaSysMenuMapper;
import com.xnyzc.lhy.mis.mapper.system.OaSysUserMapper;
import com.xnyzc.lhy.mis.service.cms.login.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  后台登陆
 * </p>
 *
 * @author wangshuo
 * @since 2019-08-08
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private OaSysUserMapper oaSysUserMapper;

    @Autowired
    private OaSysMenuMapper oaSysMenuMapper;

    /**
     * 获取登录人信息
     * @return
     */
    @Override
    public Rv getUserInfo(LoginInfo param) {
        Map<String, Object> map = new HashMap<>(16);
        Long userId=TokenUtil.getTokenUserId();
        OaSysUser user=oaSysUserMapper.selectById(userId);
        if (CheckUtil.objIsEmpty(user)){
            throw PangException.create(EErrorCode.userFieldError);
        }
        map.put("loginName", user.getLoginName());
        map.put("userType", user.getUserType());
        map.put("loginDate", user.getLoginDate());
        map.put("companyId", user.getCompanyId());
        map.put("officeId", user.getOfficeId());
        // 当前登录人的全部菜单
        List<OaSysMenu> menuList=getMenuRootList(userId);
        // 根据顶部参数获取菜单
        List<Map<String, Object>> menuMapList =new ArrayList<>();
        QueryWrapper<OaSysMenu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.like(OaSysMenu.PARENT_IDS,","+param.getMenuSelect()+",");
        List<OaSysMenu> menuSystemList = oaSysMenuMapper.selectList(menuQueryWrapper);
        // 展示不同得目录
        menuMapList=ListToTreeUtil.getMenuSelectList(menuList,menuSystemList);
        JSONArray resultList = ListToTreeUtil.listToTree(JSONArray.parseArray(JSON.toJSONString(menuMapList)),"id","pId","children");
        map.put("menuList",resultList);
        return Rv.wrap(EErrorCode.success, map);
    }

    public List<OaSysMenu> getMenuRootList(Long sysUserId){
        List<OaSysMenu> menuList=new ArrayList<>();
        if (sysUserId==1L){
            menuList = oaSysMenuMapper.menuRootList();
        }else{
            GlobalConfig config =new GlobalConfig();
            config.setUserId(sysUserId);
            menuList = oaSysMenuMapper.menuRootByUserId(config);
        }
        return menuList;
    }

    public List<OaSysMenu> getMenuList(Long sysUserId){
        List<OaSysMenu> menuList=new ArrayList<>();
        if (sysUserId==1L){
            menuList = oaSysMenuMapper.menuAllList();
        }else{
            GlobalConfig config =new GlobalConfig();
            config.setUserId(sysUserId);
            menuList = oaSysMenuMapper.menuByUserId(config);
        }
        return menuList;
    }

    /**
     * 获取登录人顶部菜单
     * @return
     */
    @Override
    public Rv getUserTopMenu() {
        Map<String, Object> map = new HashMap<>(16);
        Long userId=TokenUtil.getTokenUserId();
        OaSysUser user=oaSysUserMapper.selectById(userId);
        if (CheckUtil.objIsEmpty(user)){
            throw PangException.create(EErrorCode.userFieldError);
        }
        List<OaSysMenu> menuList=getMenuRootList(userId);
        List<Map<String, Object>> topMenuMapList =new ArrayList<>();
        // 展示不同得目录
        for(OaSysMenu menu:menuList){
            if(menu.getParentId().equals(GlobalConfig.TOP_MENU)){
                Map<String, Object> resMap = Maps.newHashMap();
                resMap.put("value", menu.getMenuId());
                resMap.put("name", menu.getName());
                topMenuMapList.add(resMap);
            }
        }
        map.put("topMenuList",topMenuMapList);
        return Rv.wrap(EErrorCode.success, map);
    }

}
